package com.mmall.controller.portal;

import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;

import com.mmall.pojo.User;
import com.mmall.service.UserService;
import com.mmall.util.CookieUtil;
import com.mmall.util.JsonUtil;
import com.mmall.util.RedisPoolUtil;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
@Api(value = "/user", description = "用户操作")
public class UserController {
    @Autowired
private UserService userService;
//
//用户登录
@ResponseBody
@RequestMapping(value="login.do")
@ApiOperation(
        value = "login.do",
        notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
        response = ServerResponse.class)

public ServerResponse<User>  login(@RequestParam("username") String username, @RequestParam("password")String password, HttpSession session, HttpServletResponse response){
   // System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.......");
    ServerResponse serverResponse = userService.login(username,password,session);
    if(serverResponse.getStatus()==ResponseCode.SUCCESS.getStatus()){
        CookieUtil.writeLoginToken(response,session.getId());
        RedisPoolUtil.set(session.getId(), JsonUtil.obj2String(serverResponse.getData()));
    }

  return serverResponse ;
}
//用户注册
@ResponseBody
@RequestMapping("register.do")
  public ServerResponse  register(User user){
    return userService.register(user);
  }
 //校验用户名是否存在
  @ResponseBody
  @RequestMapping("checkUsername.do")
  public ServerResponse  checkusername(@RequestParam("username")String username){
   return userService.checkusername(username);
  }
  //根据用户名找回问题
  @ResponseBody
  @RequestMapping("questionByusername.do")
  public ServerResponse  selectQuestionByusername(@RequestParam("username")String username){
    return  userService.selectQuestionByusername(username);
  }

  //根据用户名以及问题校验密码校验
    @ResponseBody
    @RequestMapping("answerAndusername.do")
    public ServerResponse  selectAnswerByusernameAndQuestion(@RequestParam("username")String username,
                                                             @RequestParam("question") String question,
                                                             @RequestParam("answer")String answer){
        return  userService.selectAnswerByusernameAndQuestion(username,question,answer);
    }
    //忘记密码重置密码
    @ResponseBody
    @RequestMapping("forgetResetPassword.do")
    public ServerResponse  forgetResetPassword(@RequestParam("username")String username,
                                               @RequestParam("password") String password){
        return  userService.forgetResetPassword(username,password);
    }
    //用户更新个人信息

    @ResponseBody
    @RequestMapping("updateUserInfo.do")
    public ServerResponse updateUserInfo(User user){
        return userService.updateUserInfo(user);
    }

    @ResponseBody
    @ApiOperation(nickname = "swagger-helloworld", value = "Swagger的世界", notes = "测试HelloWorld")
    @RequestMapping("getUserInfo.do")
    public ServerResponse getUserInfoById(HttpServletRequest request){
        String token = CookieUtil.readLoginToken(request);
        if(StringUtils.isBlank(token)){
            return  ServerResponse.createServerResponseByFailure("未获取到该用户相关信息");
        }

        String jsonInfo = RedisPoolUtil.get(token);
        User user = JsonUtil.string2Obj(jsonInfo,User.class);
        if(user==null){
            return  ServerResponse.createServerResponseByFailure("未登录,请登陆");
        }

        return ServerResponse.createServerResponseBySuccess(user);
    }

















}
